package com.project.systemfront.controller;


import com.project.systemfront.common.Result;
import com.project.systemfront.pojo.Comment;
import com.project.systemfront.pojo.dto.CommentDto;
import com.project.systemfront.pojo.vo.CommentVo;
import com.project.systemfront.service.IArtworkService;
import com.project.systemfront.service.ICommentService;
import com.project.systemfront.utils.JwtUtil;
import com.project.systemfront.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/system/comment")
public class CommentController
{

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IArtworkService artworkService;

    @GetMapping
    public Result<List<CommentVo>> getList(@RequestParam Long artworkId)

    {
        Comment comment = new Comment();
        comment.setArtworkId(artworkId);
        comment.setStatus(1L);
        List<CommentVo> list = commentService.selectCommentList(comment);
        return Result.success(list);
    }
    @PostMapping("/add")
    public Result<String> add(@RequestBody CommentDto commentdto)
    {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Long id=Long.parseLong(claims.get("id").toString());
        Comment comment = new Comment();
        comment.setArtworkId(commentdto.getArtworkId());
        comment.setContent(commentdto.getContent());
        comment.setUserId(id);
        comment.setStatus(1L);
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        commentService.insertComment(comment);
        artworkService.incrementCommentCount(commentdto.getArtworkId());
        return Result.success("添加成功");
    }
}
